package pyf.demo11;

import java.util.ArrayList;
import java.util.List;

/**
 * @author pengyifeng
 * @version 5.1
 * @ClassName UnSafeListDemo.java
 * @Description 线程不安全的集合
 * @createTime 2021年07月27日 11:39:00
 */
public class UnSafeListDemo {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();

        for (int i = 0; i < 99999; i++) {

            new Thread(() -> {
                synchronized (list) {
                    list.add(Thread.currentThread().getName());
                }
            }).start();

        }
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(list.size());
    }
}
